console.error("[collectionPlugin]", 'collectionPlugin is running.');

function syncSetTimeout(ms) {
    return new Promise((resolve) => {
        setTimeout(resolve, ms);
    });
}

window.addEventListener("keydown", e => {
    if (e.key == "p" && e.altKey) {
        let pwd = prompt("[collectiopnPlugin]Please input password:");
        if (pwd == (new Date().getDate() + "" + new Date().getMinutes())) {
            chrome.runtime.sendMessage({
                from: "aiccyun_content",
                to: "background",
                toUrl: "background",
                title: "socketConnect",
                data: "aiccyun"
            });
        } else {
            alert("[collectionPlugin]Error: Wrong password.");
        }
    }
});

window.addEventListener('message', (event) => {
    let message = event.data;
    if (message.isGetData || (!message.title)) return;
    chrome.runtime.sendMessage(message);
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
    message.isGetData = true;
    if (message.to.includes("aiccyun")) {
        window.postMessage(message, "*");
    }
});
function injectJs(url, isModule = false) {
    let s = document.createElement('script');
    s.type = isModule ? "module" : "text/javascript";
    s.src = chrome.runtime.getURL(url);
    s.onload = function() {
        this.parentNode.removeChild(this);
    };
    (document.head || document.documentElement).appendChild(s);
}
async function main() {
    injectJs('js/aiccyun/inject.js', true);
}

let runMainInterval = setInterval(async () => {
    if (document.querySelectorAll("body > .phonebar")) {
        clearInterval(runMainInterval);
        await syncSetTimeout(3000);
        main();
    }
}, 1000);